/**
 *
 * @param {array} a
 * @returns {boolean}
 */
export const isEmptyArray = (a) => {
	return a.length == 0
}

/**
 *
 * @param {number} min //
 * @param {number} max
 * @param {number} decimal // 小数点位数
 * @returns {number}
 */
export const getRandomInRange = (min, max, decimal) => {
	let randomNumber = Math.floor(Math.random() * (max - min + 1)) + min
	if (!decimal || randomNumber == max) return randomNumber
	randomNumber = Number(
		String(randomNumber) + '.' + String(Math.floor(Math.random() * Math.pow(10, decimal)))
	)
	return randomNumber
}

/**
 * @description 十进制转十六进制有符号位
 * @param {number} DECValue // 十进制
 * @returns {number}  // 十进制
 */
export const HEXtoDECSymbols = (DECValue) => {
	let DECValueInt = Number(DECValue)
	let hexadecimalNumber = DECValueInt.toString(16)
	let hexValue = parseInt(hexadecimalNumber, 16)
	let int16Value = new Int16Array([hexValue])[0]
	return int16Value
}

/**
 * @description
 * @param {number} m // 数值 22
 * @param {number} divisor // 底数 100
 * @returns {number}   // 0.22
 */
export const MathValueFormat = (m, divisor = 10) => {
	let num = m ?? 0
	let baseNumber = Math.log10(divisor)
	return (Number(num) / Number(divisor)).toFixed(baseNumber)
}

/**
 * @description
 * @param {number} bity // 3
 * @param {number} num // 22
 * @returns {number}   // 22.000
 */
export const initDecimalPlaces = (bity, num = 0) => {
	let numInt = Number(num).toFixed(bity)
	return numInt
}

/**
 * @description 获取静态资源
 * @param { name }
 * @returns { string }   // url
 */
export const getImageUrl = (url) => {
	const path = `../assets/image/${url}`
	return new URL(path, import.meta.url).href
}

/**
 * @description 判断字符是否为空
 * @param { value }
 * @returns { boolean }
 */
export const isEmpty = (value) => {
	return !value || !Number(value) ? false : true
}

let pi = 3.14159265358979324
let a = 6378245.0
let ee = 0.00669342162296594323
/*判断是否在国内，不在国内则不做偏移*/
function outOfChina(lon, lat) {
	if ((lon < 72.004 || lon > 137.8347) && (lat < 0.8293 || lat > 55.8271)) {
		return true
	} else {
		return false
	}
}
function transformLat(x, y) {
	let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
	ret += ((20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) / 3.0
	ret += ((20.0 * Math.sin(y * pi) + 40.0 * Math.sin((y / 3.0) * pi)) * 2.0) / 3.0
	ret += ((160.0 * Math.sin((y / 12.0) * pi) + 320 * Math.sin((y * pi) / 30.0)) * 2.0) / 3.0
	return ret
}

function transformLon(x, y) {
	let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
	ret += ((20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0) / 3.0
	ret += ((20.0 * Math.sin(x * pi) + 40.0 * Math.sin((x / 3.0) * pi)) * 2.0) / 3.0
	ret += ((150.0 * Math.sin((x / 12.0) * pi) + 300.0 * Math.sin((x / 30.0) * pi)) * 2.0) / 3.0
	return ret
}

function transform(wgLat, wgLon) {
	let mars_point = { lon: 0, lat: 0 }
	if (outOfChina(wgLat, wgLon)) {
		mars_point.lat = wgLat
		mars_point.lon = wgLon
		return
	}
	let dLat = transformLat(wgLon - 105.0, wgLat - 35.0)
	let dLon = transformLon(wgLon - 105.0, wgLat - 35.0)
	let radLat = (wgLat / 180.0) * pi
	let magic = Math.sin(radLat)
	magic = 1 - ee * magic * magic
	let sqrtMagic = Math.sqrt(magic)
	dLat = (dLat * 180.0) / (((a * (1 - ee)) / (magic * sqrtMagic)) * pi)
	dLon = (dLon * 180.0) / ((a / sqrtMagic) * Math.cos(radLat) * pi)
	mars_point.lat = wgLat + dLat
	mars_point.lon = wgLon + dLon
	return mars_point
}

/**
 * 将数组分为n份
 * @param {*} array
 * @param {*} n
 * @returns
 */

export const chunk = (array, n) => {
	if (!array || n <= 0) return []
	let result = []
	for (let i = 0; i < array.length; i += n) {
		result.push(array.slice(i, i + n))
	}
	return result
}
